package cn.com.xf.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import cn.com.xf.dao.TsUserDAO;
import cn.com.xf.domain.TsUser;
import cn.com.xf.service.AnalyzeService;
import cn.com.xf.task.AnalyzeTask;

@Service("threadAnalyzeService")
public class ThreadAnalyzeServiceImpl implements AnalyzeService {

	@Autowired
	private TsUserDAO userDAO;	

	@Autowired
	private ThreadPoolTaskExecutor taskExecutor;
	
	@Override
	public void analyze(String siteid, String deptid, String userid,
			Date startDate, Date endDate) throws Exception {
		if (!userid.equals("")) {
			if (userid.contains(",")) {
				String[] userids = userid.split(",");
				for (String u: userids) {
					taskExecutor.execute(new AnalyzeTask(u, startDate, endDate));
					
				}
			} else {
				taskExecutor.execute(new AnalyzeTask(userid, startDate, endDate));
			}
		} else if (!deptid.equals("") && !siteid.equals("")) { 
			List<TsUser> userList = userDAO.selectUserBySiteIdAndDeptid(siteid, deptid);
			for (TsUser user : userList) {
				taskExecutor.execute(new AnalyzeTask(user.getUserid(), startDate, endDate));
			}
			
		} 
		
	}

}
